package com.licq.ldoc.writer.visitor.example;

import com.licq.ldoc.domain.Attribute;
import com.licq.ldoc.domain.AttributeElement;
import com.licq.ldoc.domain.AttributeType;
import com.licq.ldoc.domain.Element;
import com.licq.ldoc.domain.ElementType;
import com.licq.ldoc.writer.visitor.Visitor;
import com.licq.ldoc.writer.visitor.example.exampler.BasicAttributeElementExampler;
import com.licq.ldoc.writer.visitor.example.exampler.BasicAttributeExampler;
import com.licq.ldoc.writer.visitor.example.exampler.MapperAttributeElementExampler;
import com.licq.ldoc.writer.visitor.example.exampler.MapperAttributeExampler;
import com.licq.ldoc.writer.visitor.example.exampler.WrapperAttributeElementExampler;
import com.licq.ldoc.writer.visitor.example.exampler.WrapperAttributeExampler;
import com.lowagie.text.BadElementException;

/**
 * 
 * @author liujian
 *
 */
public class ExampleVisitor implements Visitor {
	
	private final org.dom4j.Element element;
	
	private final Object object;

	public ExampleVisitor(org.dom4j.Element element, Object object) {
		this.element = element;
		this.object = object;
	}

	@Override
	public void visit(Element element) throws BadElementException {
		for (Attribute attribute : element.getAttributes()) {
			attribute.accept(this);
		}
		for (AttributeElement attributeElement : element.getAttributeElements()) {
			Exampler exampler = null; 
			ElementType elementType = attributeElement.getType();
			if (ElementType.isWrapperType(elementType)) {
				exampler = new WrapperAttributeElementExampler(
						this.element, object, attributeElement
				);
			} else if (ElementType.isMapperType(elementType)) {
				exampler = new MapperAttributeElementExampler(
						this.element, object, attributeElement
				);
			} else {
				exampler = new BasicAttributeElementExampler(
						this.element, object, attributeElement
				);
			}
			exampler.create();
		}
	}

	@Override
	public void visit(Attribute attribute) throws BadElementException {
		AttributeType type = attribute.getType();
		if (AttributeType.isWrapperType(type) || AttributeType.isMapperType(type)) {
			Exampler exampler = null;
			if (AttributeType.isWrapperType(type)) {
				exampler = new WrapperAttributeExampler(
						element, object, attribute.getName(), type, attribute.isRequired()
				);
			} else {
				exampler = new MapperAttributeExampler(
						element, object, attribute.getName(), type, attribute.isRequired()
				);
			}
			exampler.create();
		} else {
			Exampler exampler = new BasicAttributeExampler(
					element, object, attribute.getName(), type, attribute.isRequired()
			);
			exampler.create();
		}
	}

}
